/*
 * AdministracioServiceImpl.java
 *
 * Created on 10 de enero de 2008, 18:10
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.wooxes.server;

import org.wooxes.client.AdministracioService;
import org.wooxes.client.Activitat;
import org.wooxes.client.Configuracio;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

/**
 *
 * @author woox
 */
public class AdministracioServiceImpl extends RemoteServiceServlet implements AdministracioService{
    public Configuracio getConfiguracio(){
        ConnexioSQL connexio = new ConnexioSQL();
        Connection conn = connexio.getConnection();
        if (conn == null){
            return null;
        }
        Configuracio config = new Configuracio();
        try{
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery("SELECT * FROM configuracio");

            String st;
            while (rs.next()){
                st = rs.getString("nom");
                if( st.equals("uploaddir")){
                    config.setUploadDir(rs.getString("valor"));
                }
                else if( st.equals("maxfilesize")){
                    config.setMaxFileSize(Integer.parseInt(rs.getString("valor")));
                }
            }
        rs.close();
        statement.close();
        }
        catch(Exception e){
            System.out.println(e);
        }

        return config;
        
    }
    
    public Vector getActivitats(int i){
        ConnexioSQL connexio = new ConnexioSQL();
        Connection conn = connexio.getConnection();
        if(conn == null){
            return null;
        }
        
        Vector vc = new Vector();
        try{
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery(
                    "SELECT a.id, a.nom, a.data, a.hora, a.descripcio, a.adjunt " +
                    "FROM activitat a " +
                    "WHERE a.idassignatura = " + i + " " +
                    "ORDER BY a.nom");
            
            while (rs.next()){
                Activitat ac = new Activitat();
                
                ac.setId(rs.getInt("id"));
                ac.setNom(rs.getString("nom"));
                ac.setDescripcio(rs.getString("descripcio"));
                ac.setAdjunt(rs.getString("adjunt"));
                //Data
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date d = format.parse(rs.getString("data")+" "+rs.getString("hora"));
                ac.setData(d);
                
                vc.add(ac);
            }
            rs.close();
            statement.close();
        }
        catch(Exception e){
            System.out.println(e);
            return null;
        }

        return vc;
    }

}
